< previous page page_461 next page >

Page 461
In the LostFocus() event, you check to see whether a text box has been filled in by checking the Text property. Be sure to trim the Text property, using the Trim$() function, in case the user has entered all spaces. Next, give the user a message indicating that the field must be filled in. After the message is displayed, you want to set the focus back to the text box, because the cursor has already moved to the next field.
Deadly Embrace and the LostFocus() Event
Placing code in the LostFocus() event for a single text box is generally not a problem. However, when youre checking multiple text boxes to see whether they are empty, you can run into some big problems.
Examples Filename: TextBoxes.frm
Consider the following code thats placed in the first two text boxes on the sample form shown in Figure 23.2.
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub txtFirst_LostFocus()
   If Trim$(txtFirst.Text) = "" Then
      MsgBox "First Name Field Must Be Filled In"
      txtFirst.SetFocus
   End If
End Sub

Private Sub txtLast_LostFocus()
   If Trim$(txtLast.Text) = "" Then
      MsgBox "Last Name Field Must Be Filled In"
      txtLast.SetFocus
   End If
End Sub
8160-0461a.gif
Figure 23.2
Validation events using a sample form.

 
< previous page page_461 next page >

If you like this book, buy it!